package web.suzy.oj.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import web.suzy.oj.common.result.ResultBean;
import web.suzy.oj.pojo.dto.ProblemDTO;
import web.suzy.oj.pojo.entity.problem.Problem;
import web.suzy.oj.pojo.entity.problem.ProblemCase;
import web.suzy.oj.service.admin.problem.AdminProblemService;

import java.util.List;

/**
 * YangSuzy 软件工作室
 * 类名: AdminProblemController
 * 描述: ---- 待定 -----
 * 功能: ---- 待定 -----
 *
 * @author YangSuzy
 * Date: 2022/11/22 14:48
 */
@RestController
@RequestMapping("/api/admin/problem")
public class AdminProblemController {
    @Autowired
    private AdminProblemService adminProblemService;

    /**
     * 方法名: getProblemList
     * 描述: 获取题目列表
     *
     * @param limit       每页显示数目
     * @param currentPage 当前页
     * @param keyword     关键字
     * @param auth        题目权限 1-公开 2-私有
     * @return web.suzy.oj.common.result.ResultBean<com.baomidou.mybatisplus.core.metadata.IPage < web.suzy.oj.pojo.entity.problem.Problem>>
     * @date 2022/11/22 14:49
     * @auther YangSuzy
     **/
    @GetMapping("/get-problem-list")
    @RequiresAuthentication
    @RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
    public ResultBean<IPage<Problem>> getProblemList(@RequestParam(value = "limit", required = false) Integer limit,
                                                     @RequestParam(value = "currentPage", required = false) Integer currentPage,
                                                     @RequestParam(value = "keyword", required = false) String keyword,
                                                     @RequestParam(value = "auth", required = false) Integer auth) {
        return adminProblemService.getProblemList(limit, currentPage, keyword, auth);
    }

    /**
     * 方法名: getProblem
     * 描述: 查看题目信息
     *
     * @param pid 题目ID
     * @return web.suzy.oj.common.result.ResultBean<web.suzy.oj.pojo.entity.problem.Problem>
     * @date 2022/11/22 15:06
     * @auther YangSuzy
     **/
    @GetMapping("") //get请求
    @RequiresAuthentication
    @RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
    public ResultBean<Problem> getProblem(@RequestParam("pid") Long pid) {
        return adminProblemService.getProblem(pid);
    }

    /**
     * 方法名: deleteProblem
     * 描述: 删除题目
     *
     * @param pid 题目ID
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/22 15:36
     * @auther YangSuzy
     **/
    @DeleteMapping("") //delete请求
    @RequiresAuthentication
    @RequiresRoles(value = {"root", "problem_admin"}, logical = Logical.OR)
    public ResultBean<Void> deleteProblem(@RequestParam("pid") Long pid) {
        return adminProblemService.deleteProblem(pid);
    }

    /**
     * 方法名: addProblem
     * 描述: 添加题目
     *
     * @param problemDto 题目信息传输对象
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/22 15:50
     * @auther YangSuzy
     **/
    @PostMapping("") //post请求
    @RequiresAuthentication
    @RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
    public ResultBean<Void> addProblem(@RequestBody ProblemDTO problemDto) {
        return adminProblemService.addProblem(problemDto);
    }

    /**
     * 方法名: updateProblem
     * 描述: 更新题目信息
     *
     * @param problemDto 题目信息传输对象
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/22 16:08
     * @auther YangSuzy
     **/
    @PutMapping("") //put请求
    @RequiresAuthentication
    @RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
    public ResultBean<Void> updateProblem(@RequestBody ProblemDTO problemDto) {
        return adminProblemService.updateProblem(problemDto);
    }

    /**
     * 方法名: getProblemCases
     * 描述: 获取题目测试用例
     *
     * @param pid      题目ID
     * @param isUpload 是否使用上传的文件
     * @return web.suzy.oj.common.result.ResultBean<java.util.List < web.suzy.oj.pojo.entity.problem.ProblemCase>>
     * @date 2022/11/22 18:30
     * @auther YangSuzy
     **/
    @GetMapping("/get-problem-cases")
    @RequiresAuthentication
    @RequiresRoles(value = {"root", "admin", "problem_admin"}, logical = Logical.OR)
    public ResultBean<List<ProblemCase>> getProblemCases(@RequestParam("pid") Long pid,
                                                         @RequestParam(value = "isUpload", defaultValue = "true") Boolean isUpload) {
        return adminProblemService.getProblemCases(pid, isUpload);
    }

    /**
     * 方法名: changeProblemAuth
     * 描述: 修改题目权限
     *
     * @param problem 题目信息
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/22 18:40
     * @auther YangSuzy
     **/
    @PutMapping("/change-problem-auth")
    @RequiresAuthentication
    @RequiresRoles(value = {"root", "problem_admin"}, logical = Logical.OR)
    public ResultBean<Void> changeProblemAuth(@RequestBody Problem problem) {
        return adminProblemService.changeProblemAuth(problem);
    }
}
